<template>
  <div class="gooods-relevant">
    <div class="goods-relevant-title">同类商品推荐</div>
    <el-carousel :interval="4000" type="card" height="300px" ref="carousel">
      <el-carousel-item v-for="item in goods" :key="item.id">
        <div class="goods-relevant-box" >
          <GoodsItem :goods='item[0]'/>
          <GoodsItem :goods='item[1]'/>
        </div>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script>
import { ref, onMounted, computed, watch } from 'vue'
import GoodsItem from '../../category/components/goods-item.vue'
import {findRelGoods} from '@/api/product.js'
export default {
  name: 'GoodsRelevant',
  props: {                             
    goodsId: {
      type: String,
      defalut: ''
    }
  },
  components: {
    GoodsItem
  },
  setup (props) { 
    const carousel = ref(null)
    onMounted(()=> [ 
      carousel.value.next()
    ])
    const goods = ref([])
    const id = computed(() => {
      return props.goodsId
    })
    watch(id, () => {
      goods.value = []
      findRelGoods(id.value).then(res => {
      // 每页2条数据
      let size = 2
      for (let i=0; i<8; i++) {
        goods.value.push(res.result.slice(i*size,(i+1)*size))
      }
    })
    })
    return {
      goods,
      carousel
    }
  }
}
</script>

<style scoped lang='less'>
.goods-relevant-title {
  font-size: 20px;
  padding-top: 20px;
}
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
}
.goods-relevant-box {
  background-color: rgb(255, 255, 255);
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>